package com.example.foldertreeview.fragments;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;


import com.example.foldertreeview.FileTools;
import com.example.foldertreeview.FolderTreeView;
import com.example.foldertreeview.MessageEvent;
import com.example.foldertreeview.MsgConstant;
import com.example.foldertreeview.R;
import com.example.foldertreeview.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;


//所有fragment的父类，其中提取了很多冗余代码
public class CallPathFragment extends Fragment implements FolderTreeView.OnNodeClickListener, FolderTreeView.OnNodeCheckedListener {
    private static final String TAG = CallPathFragment.class.getSimpleName();
    //手机系统目录
    private FolderTreeView mFolderTreeView;
    //重置 路径
    private Button pathResetBtn;

    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_call_path, container, false);
        mFolderTreeView = view.findViewById(R.id.mFolderTreeView);
        mFolderTreeView.setShowRootDir(false);
        mFolderTreeView.setShowPublicDir(false);
        mFolderTreeView.setShowHidden(false); //不显示隐藏目录
        mFolderTreeView.setMultiSelect(false); //多选
        mFolderTreeView.setLineSpace(16); //行间距
        mFolderTreeView.setSelectedBkDrawable(R.drawable.item_selected); //选择项背景

        mFolderTreeView.setOnNodeClickListener(this); //目录点击监听
        mFolderTreeView.setOnNodeCheckedListener(this); //目录选择监听
        mFolderTreeView.init();

        pathResetBtn = view.findViewById(R.id.pathResetBtn);
        //重置路径
        pathResetBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                StringBuilder builder=new StringBuilder();
                //复选模式
                // for (File file : mFolderTreeView.getSelectedFolderList()) {
                //     builder.append(file.getAbsolutePath()).append('\n');
                //  }
                //单选模式
                builder.append(mFolderTreeView.getSelectedFolder()).append('\n');
                //列出选中目录下所有 音频文件
                File file = mFolderTreeView.getSelectedFolder();
                if (file!=null){
                    //保存设置
                    SharedPreferencesUtils.setCallRecordPath(mContext,file.getAbsolutePath());
                    //列出所有 电话录音
                    List<File> fileList = FileTools.searchFiles(file);
                    for (File file1 : fileList) {
                        builder.append(file1.getName()).append('\n');
                    }
                    new AlertDialog.Builder(mContext)
                            .setTitle("已选择：")
                            .setMessage(builder)
                            .create()
                            .show();

//                    发送信息
                    MessageEvent msg = new MessageEvent(MsgConstant.query_type,1);
                    msg.setMsg(file.getAbsolutePath());
                    EventBus.getDefault().post(msg);
                }else{
                    new AlertDialog.Builder(mContext)
                            .setTitle("提示")
                            .setMessage("请选择文件夹")
                            .create()
                            .show();
                }

            }
        });
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //数据源
    }

    @Override
    public void onNodeClick(FolderTreeView view, int position, File dir, boolean isExpanded) {
        Log.e(TAG, "dir name: " + dir.getName() + ", isOpen: " + isExpanded);
    }

    @Override
    public void onNodeChecked(FolderTreeView view, int position, File dir, boolean isChecked) {
        Log.e(TAG, "dir name: " + dir.getName() + ", isChecked: " + isChecked);

    }
}
